﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WaterRegimenMonitor.Web.DataModel;
using Telerik.Web.Mvc;
using WaterRegimenMonitor.Web.Models.Station;
using WaterRegimenMonitor.Common;
using WaterRegimenMonitor.Web.Code;

namespace WaterRegimenMonitor.Web.Domain
{
    /// <summary>
    /// 测站辅助类
    /// </summary>
    public static class StationHelper
    {
        /// <summary>
        /// 获得当前用户授权的测站列表。
        /// </summary>
        /// <returns></returns>
        public static List<t_st> GetOwnedStations()
        {
            //获得当前用户
            UserPrincipal principal = Helper.CurrentPrincipal;

            //判断是否有效
            if (null == principal)
            {
                return null;
            }

            //获得当前用户所属的组织结构
            List<Organization> organizations = OrganizationHelper.GetOrganizationList(principal.User.Organization_ID);

            //用于保存组织结构
            Dictionary<string, Organization> map = new Dictionary<string, Organization>();

            //添加组织结构
            foreach (Organization item in organizations)
            {
                //添加到映射
                map.Add(item.Organization_Code, item);
            }

            //用于保存测站
            List<t_st> stations = new List<t_st>();

            //查询数据
            using (TalimuEntities context = new TalimuEntities())
            {
                //循环添加
                foreach (t_st item in context.t_st)
                {
                    //判断是否需要添加
                    if (map.ContainsKey(item.unit_code.Trim()))
                    {
                        //添加
                        stations.Add(item);
                    }
                }
            }

            //返回结果
            return stations;
        }

        /// <summary>
        /// 获得当前用户授权的组织列表。
        /// </summary>
        /// <returns></returns>
        public static List<Organization> GetOwnedOrganizations()
        {
            //获得当前用户
            UserPrincipal principal = Helper.CurrentPrincipal;

            //判断是否有效
            if (null == principal)
            {
                return null;
            }

            //获得当前用户所属的组织结构
            return OrganizationHelper.GetOrganizationList(principal.User.Organization_ID);
        }

        /// <summary>
        /// 查询测站水情。
        /// </summary>
        /// <param name="query">查询信息。</param>
        /// <returns>测站水情。</returns>
        public static GridModel<StationWaterListRow> QueryStationWater(JosnPagingQueryModel query)
        {
            //打开数据上下文
            using (TalimuEntities context = new TalimuEntities())
            {
                //创建查询
                var result = UIHelper.GetGridModelFromQueryModel<WaterRegimenMonitor.Web.DataModel.st_water>
                    (
                        context.st_water,
                        query.page,
                        query.limit,
                        UIHelper.GetCommonSortDescriptorFromSortModel(query.SortModel),
                        UIHelper.GetCommonFilterFromFilterModel(query.FilterModel)
                    );

                //获得结果集
                var list = result.Data.Select(item => new StationWaterListRow()
                {
                    dt = item.dt,
                    name = string.Empty,
                    st_code = item.st_code,
                    w_flow = item.w_flow,
                    w_level = item.w_level,
                    dtsource = item.dtsource
                }).ToList<StationWaterListRow>();

                //返回结果
                return new GridModel<StationWaterListRow>()
                {
                    Aggregates = result.Aggregates,
                    Data = list,
                    Total = result.Total
                };
            }
        }

        /// <summary>
        /// 查询最新水情。
        /// </summary>
        /// <param name="query">查询信息。</param>
        /// <returns>最新水情。</returns>
        public static GridModel<StationWaterListRow> QueryNewlyWater(JosnPagingQueryModel query)
        {
            //打开数据上下文
            using (TalimuEntities context = new TalimuEntities())
            {
                //创建查询
                var result = UIHelper.GetGridModelFromQueryModel<WaterRegimenMonitor.Web.DataModel.NewlyReportView>
                    (
                        context.NewlyReportView,
                        query.page,
                        query.limit,
                        UIHelper.GetCommonSortDescriptorFromSortModel(query.SortModel),
                        UIHelper.GetCommonFilterFromFilterModel(query.FilterModel)
                    );

                //获得结果集
                var list = result.Data.Select(item => new StationWaterListRow()
                {
                    dt = item.dt,
                    name = item.st_name,
                    st_code = item.st_code,
                    w_flow = item.w_flow,
                    w_level = item.w_level,
                    dtsource = 0
                }).ToList<StationWaterListRow>();

                //返回结果
                return new GridModel<StationWaterListRow>()
                {
                    Aggregates = result.Aggregates,
                    Data = list,
                    Total = result.Total
                };
            }
        }

        ///// <summary>
        ///// 查询测站水情。
        ///// </summary>
        ///// <param name="query">查询信息。</param>
        ///// <returns>测站水情。</returns>
        //public static GridModel<StationWaterListRow> QueryStationDailyWater(JosnPagingQueryModel query)
        //{
        //    //打开数据上下文
        //    using (TalimuEntities context = new TalimuEntities())
        //    {
        //        //创建查询
        //        var result = UIHelper.GetGridModelFromQueryModel<WaterRegimenMonitor.Web.DataModel.st_flow>
        //            (
        //                context.st_flow,
        //                query.page,
        //                query.limit,
        //                UIHelper.GetCommonSortDescriptorFromSortModel(query.SortModel),
        //                UIHelper.GetCommonFilterFromFilterModel(query.FilterModel)
        //            );

        //        //获得结果集
        //        var list = result.Data.Select(item => new StationWaterListRow()
        //        {
        //            dt = item.dt,
        //            name = string.Empty,
        //            st_code = item.st_code,
        //            w_flow = item.,
        //            w_level = item.w_level
        //        }).ToList<StationWaterListRow>();

        //        //返回结果
        //        return new GridModel<StationWaterListRow>()
        //        {
        //            Aggregates = result.Aggregates,
        //            Data = list,
        //            Total = result.Total
        //        };
        //    }
        //}
    }
}